package com.tzxs.pay.main.onlineaut;

import android.app.Activity;
import android.app.ProgressDialog;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Menu;

import com.tzxs.pay.R;
import com.tzxs.pay.databinding.ItemOnlineAutDetailBinding;
import com.tzxs.pay.main.base.BaseActivity;
import com.tzxs.pay.main.bill.viewmodel.BillViewModel;
import com.tzxs.pay.main.onlineaut.model.OnlineAutPrint;
import com.tzxs.pay.main.onlineaut.model.OnlineDetailBean;
import com.tzxs.pay.main.print.utils.print.AidlUtil;
import com.tzxs.pay.util.StatusBarColorSetter;

//在线预授权详情页
public class OnlineAdActivity extends BaseActivity<ItemOnlineAutDetailBinding> {
    private static final String TAG = OnlineAdActivity.class.getSimpleName();
    private BillViewModel model;
    private ProgressDialog dialog;
    private String orderId;

    @Override
    protected Activity getChildActivity() {
        return this;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.item_online_aut_detail;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarColorSetter.setStatusBarColor(this, Color.parseColor("#FAFAFA"));
        sendBroadcast1();
        //back事件
        dataBinding.detailToolbar.setTitle("");
        setSupportActionBar(dataBinding.detailToolbar);
        dataBinding.detailToolbar.setNavigationOnClickListener(v -> onBackPressed());
        //打印
        dataBinding.detailToolbar.setOnMenuItemClickListener(item -> {
            if (item.getItemId() == R.id.print) {
                model.getOnlineAutPrint(getIntent().getStringExtra("orderId"), "1", () -> {
                    if (model.printBean.get() != null) {
                        OnlineAutPrint.DataBean bean = model.printBean.get();
                        printContent(bean);
                    }
                });
                return true;
            }
            return false;
        });
        model = ViewModelProviders.of(this).get(BillViewModel.class);
        dataBinding.setViewModel(model);
        orderId = getIntent().getStringExtra("orderId");
        //获取详情
        model.getOnlineAutDetail(getIntent().getStringExtra("orderId"), "0", () -> {
            dialog.dismiss();
            if (model.onlineAutDetail.get() != null) {
                OnlineDetailBean.DataBean bean = model.onlineAutDetail.get();
                dataBinding.payStatus.setText(bean.getStatusTypeText());
            }
        });

        model.dialogText.observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                if (s == null) {
                    if (dialog != null) {
                        dialog.dismiss();
                    }
                } else {
                    dialog = ProgressDialog.show(OnlineAdActivity.this, s, "请稍等....", false, false);
                }
            }
        });
        model.snackbarText.observe(this, s -> {
            if (s != null) {
                Log.d(TAG, "onCreate: OnlineAdActivity:显示消息:" + s);
                Snackbar.make(dataBinding.detailToolbar, s, Snackbar.LENGTH_SHORT).show();
            }
        });
        //重发短信
        dataBinding.resendMsg.setOnClickListener(v -> {
            model.resendMsg(orderId, () -> {
            });
        });
        //订单核销
        dataBinding.orderCancel.setOnClickListener(v -> {
            showDialog();
        });
    }

    private void printContent(OnlineAutPrint.DataBean bean) {
        String printContent = bean.getPrint_body();
        String result = printContent.replace("\\n", "\n");
        AidlUtil aidlUtil = AidlUtil.getInstance();
        aidlUtil.enterPrinterBuffer(true);
        aidlUtil.printText(bean.getStore_name(), "32", "32", "1", true, false);
        aidlUtil.printBlankLine("1", "10");
        aidlUtil.printText(result, "24", "24", "0", false, false);
        aidlUtil.printQr(bean.getOut_order_no(), "6", "3");
        aidlUtil.printBlankLine("1", "10");
        aidlUtil.printText("扫一扫订单号", "24", "24", "1", false, false);
        aidlUtil.printBlankLine("1", "10");
        aidlUtil.printText("天择可付      智慧收银", "24", "24", "1", false, false);
        aidlUtil.printBlankLine("5", "30");
        aidlUtil.printCommit();
    }

    //显示确认到店dialog
    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle("核对到店")
                .setTitle("是否确认到店")
                .setPositiveButton("确认", (dialog, which) -> {
                    model.cancelOrder(orderId, () -> {
                        dataBinding.orderCancel.setEnabled(false);
                        dataBinding.resendMsg.setEnabled(false);
                        dataBinding.payStatus.setText("已核对");
                    });
                    dialog.dismiss();
                }).setNegativeButton("取消", (dialog, which) -> {
                    dialog.dismiss();
                });
        builder.show();
    }

    //发送广播
    private void sendBroadcast1() {
        Intent intent = new Intent("com.tzxs.pay.main.onlineaut.broadcast.AutFinishBroadCast");
        sendBroadcast(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.print_, menu);
        return true;
    }
}